/**
 * AdminServlet
 * Servlet for administering the ecom application.
 * @author    Fabienne Boyer - Didier Donsez - may 2006
 */
package ecom.servlets;


import ecom.session.EcomAdminRemote;
import java.io.IOException;
import javax.ejb.EJB;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AdminServlet extends HttpServlet {

    @EJB
    private EcomAdminRemote adminBean;
    
    /**
     * Serializable class uid.
     */
    private static final long serialVersionUID = 6893863749912962928L;

    /**
     * Called by the server (via the service method) to allow a servlet to
     * handle a GET request.
     * @param request an HttpServletRequest object that contains the request
     * the client has made of the servlet
     * @param response an HttpServletResponse object that contains the
     * response the servlet sends to the client
     * @throws IOException if an input or output error is detected when the
     * servlet handles the GET request
     * @throws ServletException if the request for the GET could not be handled
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        
        if(userPath.equals("/secured/")) {
            // Afficher l'index
        } else if (userPath.equals("/secured/customers")) {
            // Afficher le tableau des customers
        } else if (userPath.equals("/secured/products")) {
            // Afficher le tableau des products
        } else if (userPath.equals("/secured/stores")) {
            // Afficher le tableau des stores
        }
        
        // redirige la page en fonction de userPath
        redirection(request, response, userPath);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();

        
        redirection(request, response, userPath);
    }
    
        /**
     * Permet de rediriger le controller vers la bonne jsp
     * Les JSPs sont localisees dans le dossier WEB-INF
     * @param request
     * @param response
     * @param userPath 
     */
    private void redirection(HttpServletRequest request, HttpServletResponse response, String userPath) {
        // on utilise un requestdispatcher pour changer de vue
        String url;
        if (userPath.equals("/secured/") || userPath.equals("/secured/index.jsp")) {
            url = "/WEB-INF/admin/index.jsp";
        } else {        
            url = "/WEB-INF/admin" + userPath.substring(8) + ".jsp";
        }

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
